﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MotionCS.StatusLed
{
    public class StatusLed
    {
        /// <summary>
        /// LED的id
        /// </summary>
        private Int32 id;
        /// <summary>
        /// 计数值
        /// </summary>
        private Int32 count;
        /// <summary>
        /// 闪烁周期
        /// </summary>
        private Int32 cycle;
        /// <summary>
        /// 光亮时间
        /// </summary>
        private Int32 light;
        /// <summary>
        /// LED控制回调
        /// </summary>
        private Action<int, int> cbLedCtl;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="ledctl"></param>
        public StatusLed(Int32 Id, Action<int, int> ledctl)
        {
            this.id = Id;
            this.cbLedCtl = ledctl;
        }
        /// <summary>
        /// 设置周期
        /// </summary>
        /// <param name="cyc"></param>
        /// <param name="lht"></param>
        public void SetPeriod(Int32 cyc, Int32 lht)
        {
            this.cycle = cyc;
            this.light = lht;
            this.count = cyc;
        }
        /// <summary>
        /// 更新LED状态
        /// </summary>
        public void UpdateLedStatus()
        {
            this.count--;
            if (this.count == 0)
            {
                this.count = this.cycle;
                this.cbLedCtl(this.id, 0);
            }
            if (this.count == this.light)
            {
                this.cbLedCtl(this.id, 1);
            }
        }
    }
}
